package cn.hsp.demo

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.IconButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.hsp.demo.ui.theme.HspDemoTheme

/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：蓝不蓝编程
 */

class MainActivity : ComponentActivity() {
    private val interval = 50L
    private val handler = Handler(Looper.getMainLooper())
    private val ballNumUtil = BallNumUtil()
    private var ball1 = mutableStateOf("00")
    private var ball2 = mutableStateOf("00")
    private var ball3 = mutableStateOf("00")
    private var ball4 = mutableStateOf("00")
    private var ball5 = mutableStateOf("00")
    private var ball6 = mutableStateOf("00")
    private var ball7 = mutableStateOf("00")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            HspDemoTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Text(
                        "双色球-蓝不蓝编程",
                        textAlign = TextAlign.Center,
                        modifier = Modifier.padding(10.dp),
                        fontSize = 16.sp
                    )
                    HspPage(ball1, ball2, ball3, ball4, ball5, ball6, ball7) { isToStart ->
                        if (isToStart) {
                            startTimer()
                        } else {
                            pauseTimer()
                        }
                    }
                }
            }
        }
    }

    private fun startTimer() {
        handler.postDelayed({ changeBalls() }, interval)
    }

    private fun pauseTimer() {
        handler.removeCallbacksAndMessages(null)
    }

    private fun changeBalls() {
        ball1.value = ballNumUtil.blueBallNum
        ball2.value = ballNumUtil.blueBallNum
        ball3.value = ballNumUtil.blueBallNum
        ball4.value = ballNumUtil.blueBallNum
        ball5.value = ballNumUtil.blueBallNum
        ball6.value = ballNumUtil.blueBallNum
        ball7.value = ballNumUtil.redBallNum
        handler.postDelayed({ changeBalls() }, interval)
    }
}

@Composable
fun HspPage(
    _ball1: MutableState<String>,
    _ball2: MutableState<String>,
    _ball3: MutableState<String>,
    _ball4: MutableState<String>,
    _ball5: MutableState<String>,
    _ball6: MutableState<String>,
    _ball7: MutableState<String>,
    callBack: (Boolean) -> Unit
) {
    var isStarted by remember { mutableStateOf(false) }
    val ball1 by remember { _ball1 }
    val ball2 by remember { _ball2 }
    val ball3 by remember { _ball3 }
    val ball4 by remember { _ball4 }
    val ball5 by remember { _ball5 }
    val ball6 by remember { _ball6 }
    val ball7 by remember { _ball7 }

    Column(
        modifier = Modifier
            .fillMaxHeight()
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier
                .align(CenterHorizontally)
                .weight(1f)
        ) {
            getRedBall(ball1)
            getRedBall(ball2)
            getRedBall(ball3)
            getRedBall(ball4)
            getRedBall(ball5)
            getRedBall(ball6)
            getBlueBall(ball7)
        }
        IconButton(
            onClick = {
                isStarted = !isStarted
                callBack(isStarted)
            },
            modifier = Modifier
                .padding(bottom = 50.dp)
                .align(CenterHorizontally),
        ) {
            Image(
                painter = getButtonImage(isStarted),
                contentDescription = "",
            )
        }
    }

}

@Composable
private fun getRedBall(ball2: String) {
    Text(
        ball2,
        color = Color.White,
        textAlign = TextAlign.Center,
        modifier = Modifier
            .background(color = Color.Blue, shape = CircleShape)
            .width(40.dp)
            .height(40.dp)
            .padding(5.dp)
    )
}

@Composable
private fun getBlueBall(ball1: String) {
    Text(
        ball1,
        color = Color.White,
        textAlign = TextAlign.Center,
        modifier = Modifier
            .background(color = Color.Red, shape = CircleShape)
            .width(40.dp)
            .height(40.dp)
            .padding(5.dp)
    )
}

@Composable
private fun getButtonImage(isStarted: Boolean) =
    if (isStarted) painterResource(R.drawable.ic_pause) else painterResource(R.drawable.ic_start)